package com.fyp.ptma.model;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

	public static void main(String[] args) {

		//  TestDB();
		//getImages();
	}

	static void getImages() {
		try {
			BufferedReader br = new BufferedReader(
					new FileReader(
							"/Volumes/DataDrive/FYP/PtmaWar/src/main/webapp/resources/css/home.css"));
			String line = null;
			Pattern ptr = Pattern.compile("/img/(.+)");

			while ((line = br.readLine()) != null) {

				try {

					Matcher m = ptr.matcher(line);
					if (m.find()) {
						line = line.trim();
						int start = line.indexOf("/");
						int end = 0;

						if (line.contains(".png")) {

							end = line.trim().indexOf(".png") + 4;
						} else {
							end = line.trim().indexOf(".jpg") + 4;
						}

						URL website = new URL(
								"https://d3qktfj96j46kx.cloudfront.net/tonyhawk128/img/"
										+ line.substring(start + 5, end));
						ReadableByteChannel rbc = Channels.newChannel(website
								.openStream());
						FileOutputStream fos = new FileOutputStream(
								"/Volumes/DataDrive/FYP/PtmaWar/src/main/webapp/resources/images/common/"
										+ line.substring(start + 5, end));
						fos.getChannel().transferFrom(rbc, 0, 1 << 24);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

//	static void TestDB() {
//
//		Session session = HibernateSessionFactory.getSession();
//
//		session.beginTransaction();
//		User user = new User();
//		user.setDate(new Date(System.currentTimeMillis()));
//		user.setEmail("Yahya.arshad@gmail.com");
//		user.setFirstName("Yahya");
//		user.setLastName("Arshad");
//		user.setPassword("CHACAHCHA");
//
//		Application app = new Application();
//		app.setAboutApp("This is test about description of application");
//		app.setApplicationName("Great App");
//		app.setNameSpace("com.test.my.app");
//
//		ApplicationVersion appVersion = new ApplicationVersion();
//		appVersion.setSequanceNo(1);
//		appVersion.setVersionCode("1.0.1");
//		appVersion.setDate(new Date(System.currentTimeMillis()));
//		appVersion.setVersionComment("This is first Version");
//
//		appVersion.setApplication(app);
//
//		app.getAppVersions().add(appVersion);
//
//		Group group = new Group();
//		group.setDate(new Date(System.currentTimeMillis()));
//		group.setGroupName("Tester Group");
//
//		Downloader downloader = new Downloader();
//		downloader.setDeviceId("12342434234");
//		downloader.setDeviveOs("2.2");
//		downloader.setEmail("Tester@yahoo.com");
//		downloader.setFirstName("Helena");
//		downloader.setLastName("Helena Last Name");
//
//		Invitation invitation = new Invitation();
//		invitation.setAcceptanceCode("acceptanceCode");
//		invitation.setDownloader(downloader);
//		downloader.setInvitation(invitation);
//		downloader.setPhoneNo("2345442323");
//		Installation installation = new Installation();
//		installation.setApplication(app);
//		installation.setAppVersion(appVersion);
//		downloader.getInstallations().add(installation);
//		group.setApplication(app);
//		group.getDownloaders().add(downloader);
//		app.getGroups().add(group);
//
//		app.setUser(user);
//		session.save(user);
//
//		session.save(app);
//
//		session.getTransaction().commit();
//		session.close();
//	}

}
